extends Control

@onready var back_btn : Button = $ColorRect/MarginContainer/VBoxContainer/HBoxContainer/BackBtn
@onready var change_catalog_btn : Button = $ColorRect/MarginContainer/VBoxContainer/CenterContainer/VBoxContainer/ChangeCatalogBtn
@onready var create_catalog_btn : Button = $ColorRect/MarginContainer/VBoxContainer/CenterContainer/VBoxContainer/CreateCatalogBtn
@onready var edit_catalog_btn : Button = $ColorRect/MarginContainer/VBoxContainer/CenterContainer/VBoxContainer/EditCatalogBtn
#ЗАКОНЧИЛ ЗДЕСЬ НА ЗАКРЫТИИ КАТАЛОГА ПРИ СОЗДАНИИ НОВОГО

func _ready():
	
	change_catalog_btn.pressed.connect(sellect_catalog)
	create_catalog_btn.pressed.connect(_on_create_new_catalog)
	back_btn.pressed.connect(_go_to_main_page)
	edit_catalog_btn.pressed.connect(_on_catalog_edit)

func _input(event):
	if event.is_action_pressed("ui_cancel"):
		_go_to_main_page()


func _go_to_main_page():
	get_tree().change_scene_to_file("res://main_page/main_page.tscn")

func sellect_catalog():
	ENV.open_file_window(select_catalog_path, PackedStringArray(["*.catalog"]))

func select_catalog_path(path:String):
	$ColorRect/MarginContainer/VBoxContainer/HBoxContainer/CatalogDataLabel.fill_label()
	ENV.current_catalog_path = path

func _on_create_new_catalog():
	DbAdapter.new_catalog()

func _on_catalog_edit():
	if not ENV.current_catalog_path.is_empty():
		get_tree().change_scene_to_file("res://preferences_page/edit_catalog_page/edit_catalog_page.tscn")
	else:
		var message : Label = $ColorRect/MarginContainer/VBoxContainer/CenterContainer/VBoxContainer/CatalogIsNotSellectedLbl
		message.visible = true
		var timer := Timer.new()
		message.add_child(timer)
		timer.wait_time = 2
		timer.timeout.connect(func():message.visible = false;message.remove_child(timer))
		timer.start()
